package com.shsxt.crm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shsxt.common.util.Result;
import com.shsxt.crm.dao.YgCusDevPlanDao;
import com.shsxt.crm.model.YgCusDevPlan;
import com.shsxt.crm.service.ICusDevPlanService;

@Service
public class CusDevPlanServiceImpl implements ICusDevPlanService {

	@Autowired
	private YgCusDevPlanDao ygCusDevPlanDao;

	@Override
	public Map<String, Object> queryCusDevPlansBySaleChanceId(YgCusDevPlan ygCusDevPlan) {

		// 1. 设置分页参数
		PageHelper.startPage(ygCusDevPlan.getPage(), ygCusDevPlan.getRows());
		// 2. 查询数据
		Map<String, Object> params = new HashMap<>();

		params.put("saleChanceId", ygCusDevPlan.getSaleChanceId());
		params.put("isValid", 1);
		List<YgCusDevPlan> info = ygCusDevPlanDao.find(params);
		PageInfo<YgCusDevPlan> pageInfo = new PageInfo<>(info);
		// 3. 组装返回数据
		Map<String, Object> map = new HashMap<>();

		map.put("total", pageInfo.getTotal());
		map.put("rows", pageInfo.getList());

		return map;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Result insertCusDevPlans(YgCusDevPlan ygCusDevPlan) {
		// 设置创建时间
		ygCusDevPlan.setCreateDate(new Date());
		ygCusDevPlan.setUpdateDate(new Date());
		ygCusDevPlan.setIsValid(1);

		long ste = ygCusDevPlanDao.saveSte(ygCusDevPlan);
		if (ste == 1) {
			return Result.success("添加成功");
		}
		return Result.fail("添加失败");
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Result delCusDevPlans(Integer id) {

		YgCusDevPlan ygCusDevPlan = new YgCusDevPlan();
		ygCusDevPlan.setId(id);
		ygCusDevPlan.setIsValid(0);
		ygCusDevPlan.setUpdateDate(new Date());
		long ste = ygCusDevPlanDao.updateSte(ygCusDevPlan);
		if (ste == 1) {
			return Result.success("删除成功");
		}
		return Result.fail("删除失败");
	}

}
